package com.teamall.web.main.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.teamall.core.common.BaseController;
import com.teamall.core.common.CommonEnum;
import com.teamall.core.common.JModelAndView;
import com.teamall.framework.exception.BaseAppException;
import com.teamall.framework.log.LogManager;
import com.teamall.framework.util.SecurityUtils;
import com.teamall.web.main.vo.IndexTree;
import com.teamall.web.tssy.db.po.TsSyResourcePO;
import com.teamall.web.tssy.db.po.TsSyUserPO;
import com.teamall.web.tssy.service.ITsSyResourceService;
import com.teamall.web.tssy.service.ITsSyUserService;

/**
 * <Description>tuser管理 <br>
 * 
 * @author codeCreater <br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2014年11月11日 <br>
 * @since V1.0<br>
 * @see com.teamall.web.user.controller <br>
 */

@Controller
@RequestMapping("/")
public class IndexController extends BaseController{
	@Autowired
    private ITsSyResourceService tsSyResourceService;
	@Autowired
    private ITsSyUserService tsSyUserService;
	
	private static final LogManager logger = LogManager.getLogger(IndexController.class);
	@RequestMapping("index")
	public ModelAndView  index(HttpServletRequest request) throws BaseAppException {
		logger.debug("yes,debug");
		logger.info("yes,info");
		logger.error("yes,error");
		logger.warn("yes,warn");
		//request.getSession().setAttribute("loginSession",LOGIN_SESSION);//TODO 临时使用，待后期登录权限完善后再变更
		ModelAndView mv = new JModelAndView("index",request);
		//Object treeObj = request.getSession().getAttribute(PERMISSION_KEY);
		//Object userInfoObj = request.getSession().getAttribute(LOGIN_SESSION_KEY);

		return mv;
	}

	/**
	 * 
	* @Title: logout 
	* @Description: TODO(退出系统) 
	* @param request
	* @return    设定文件 
	* @throws
	 */
	@RequestMapping("code")
	public ModelAndView code(HttpServletRequest request) {

		ModelAndView mv = new JModelAndView("main/code",request);
		return mv ;
	}
	

	@RequestMapping("loginSubmit")
	public ModelAndView loginSubmit(HttpServletRequest request) throws BaseAppException {

		String loginName = request.getParameter("loginName");
		String passwordMD5 = "";
		try {
			passwordMD5 = SecurityUtils.MD5(request.getParameter("passwordhash"));
		} catch (Exception e) {
			return new JModelAndView("login", "error", "用户名或密码错误。",request);
		}
		TsSyUserPO record = new TsSyUserPO();
		record.addNewConditions("loginName", loginName);
		record.addNewConditions("password", passwordMD5);
		record.addNewConditions("isDel", CommonEnum.ISDEL_F.getCode().toString());
		record.addNewConditions("isEnable", CommonEnum.ISENABLE_T.getCode().toString());
		List<TsSyUserPO> userList = tsSyUserService.selectByArg(record);
		if (userList.size()==1) {
			TsSyUserPO userInfo = userList.get(0);
			userInfo.setPassword("");
			request.getSession().setAttribute(BaseController.LOGIN_SESSION_KEY, userInfo);

			TsSyResourcePO resourcePO = new TsSyResourcePO();
			resourcePO.addNewConditions("isDel", CommonEnum.ISDEL_F.getCode().toString()); 
			resourcePO.addNewConditions("resourceType", CommonEnum.RESOURCETYPE_MENU.getCode().toString());
			List<TsSyResourcePO> nodes = null;

			//登陆成功，获取用户权限
			if(SYSTEM_ADMIN.equals(userInfo.getLoginName())){//管理员拥有所有权限
				nodes = tsSyResourceService.selectByArg(resourcePO);
			}else{
				nodes = tsSyResourceService.selectResourceByUser(userInfo.getId(),CommonEnum.RESOURCETYPE_MENU.getCode().toString());
			}
			System.out.println(request.getContextPath());
			String treeStr = new IndexTree(nodes).buildTree(request.getContextPath());
			request.getSession().setAttribute(PERMISSION_KEY, treeStr);
			ModelAndView mv = new JModelAndView("redirect:index",request);
			return mv;
		}
		return new JModelAndView("login", "error", "用户名或密码错误。",request);
	}

	/**
	 * 
	* @Title: logout 
	* @Description: TODO(退出系统) 
	* @param request
	* @return    设定文件 
	* @throws
	 */
	@RequestMapping("logout")
	public ModelAndView logout(HttpServletRequest request) {

		request.getSession().removeAttribute(LOGIN_SESSION_KEY);
		request.getSession().removeAttribute(PERMISSION_KEY);
		ModelAndView mv = new JModelAndView("login",request);
		return mv ;
	}
	
	/**
	 * 
	* @Title: logout 
	* @Description: TODO(退出系统) 
	* @param request
	* @return    设定文件 
	* @throws
	 */
	@RequestMapping("toMainTab")
	public ModelAndView toMainTab(HttpServletRequest request) {
		ModelAndView mv = new JModelAndView("index_layout",request);
		return mv ;
	}
	
	/**
	 * 
	* @Title: login 
	* @Description: TODO(系统登录界面) 
	* @param request
	* @return    设定文件 
	* @throws
	 */
	@RequestMapping("login")
	public ModelAndView login(HttpServletRequest request) {
		Object treeObj = request.getSession().getAttribute(PERMISSION_KEY);
    	Object userInfoObj = request.getSession().getAttribute(LOGIN_SESSION_KEY);
    	ModelAndView mv = null;
    	if(treeObj==null || userInfoObj==null){
    	}else{
    		mv = new JModelAndView("redirect:index",request);
    	}
    	mv = new JModelAndView("login",request);
		
		return mv ;
	}
	
}
